package rpc.utils;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*********
 *
 *
 *@author 197
 *@date 2020/6/19 16:06
 *
 **/
public class TypeSafe {

  public TypeSafe() {
  }

  public static String anyToString(Object v) {
    if (v == null) {
      return null;
    } else {
      return v instanceof String ? (String) v : v.toString();
    }
  }

  public static String anyToString(Object v, String dv) {
    if (v == null) {
      return dv;
    } else {
      return v instanceof String ? (String) v : v.toString();
    }
  }

  public static int anyToInt(Object v) {
    return anyToInt(v, 0);
  }

  public static int anyToInt(Object v, int dv) {
    if (v == null) {
      return dv;
    } else if (v instanceof Integer) {
      return (Integer) v;
    } else if (v instanceof Number) {
      return ((Number) v).intValue();
    } else {
      try {
        return Double.valueOf(v.toString()).intValue();
      } catch (Exception var3) {
        return dv;
      }
    }
  }

  public static float anyToFloat(Object v) {
    return anyToFloat(v, 0.0F);
  }

  public static float anyToFloat(Object v, float dv) {
    if (v == null) {
      return dv;
    } else if (v instanceof Float) {
      return (Float) v;
    } else if (v instanceof Number) {
      return ((Number) v).floatValue();
    } else {
      try {
        return Double.valueOf(v.toString()).floatValue();
      } catch (Exception var3) {
        return dv;
      }
    }
  }

  public static boolean anyToBool(Object v) {
    return anyToBool(v, false);
  }

  public static boolean anyToBool(Object v, boolean dv) {
    if (v != null && !"".equals(v)) {
      if (v instanceof Boolean) {
        return (Boolean) v;
      } else if (v instanceof Number) {
        return ((Number) v).intValue() == 1;
      } else {
        try {
          String s = v.toString().toLowerCase();
          if (!s.equals("true") && !s.equals("yes") && !s.equals("t") && !s.equals("y") && !s
              .equals("1")) {
            return !s.equals("false") && !s.equals("no") && !s.equals("f") && !s.equals("n") && !s
                .equals("0") ? dv : false;
          } else {
            return true;
          }
        } catch (Exception var3) {
          return dv;
        }
      }
    } else {
      return dv;
    }
  }

  public static double anyToDouble(Object v) {
    return anyToDouble(v, 0.0D);
  }

  public static double anyToDouble(Object v, double dv) {
    if (v == null) {
      return dv;
    } else if (v instanceof Double) {
      return (Double) v;
    } else if (v instanceof Number) {
      return ((Number) v).doubleValue();
    } else {
      try {
        return Double.parseDouble(v.toString());
      } catch (Exception var4) {
        return 0.0D;
      }
    }
  }

  public static BigInteger anyToBigInteger(Object v) {
    if (v == null) {
      return BigInteger.ZERO;
    } else {
      try {
        return new BigInteger(v.toString());
      } catch (Exception var2) {
        return BigInteger.ZERO;
      }
    }
  }

  public static Map<String, Object> anyToMap(Object v) {
    if (v == null) {
      return null;
    } else {
      return v instanceof Map ? (Map<String, Object>) v : null;
    }
  }

  public static List<Object> anyToList(Object v) {
    if (v == null) {
      return null;
    } else {
      return v instanceof List ? (List) v : null;
    }
  }

  public static Date anyToDate(Object v) {
    if (v == null) {
      return null;
    } else if (v instanceof Date) {
      return (Date) v;
    } else if (v instanceof Long) {
      return new Date((Long) v);
    } else if (v instanceof String) {
      String s = (String) v;
      if (s.isEmpty()) {
        return null;
      } else if (s.contains("-")) {
        return parseDate(s);
      } else {
        long l = anyToLong(s);
        return l == 0L ? null : new Date(l);
      }
    } else {
      return null;
    }
  }

  static Date parseDate(String s) {
    try {
      if (s.length() == 19) {
        return DateUtils.parse(s);
      } else {
        if (s.length() != 10) {
          return null;
        }
        return DateUtils.parse(s, DateUtils.getSimpleDateFormatThreadLocal1());
      }
    } catch (Exception var2) {
      return null;
    }
  }

  public static long anyToLong(Object v) {
    return anyToLong(v, 0L);
  }

  public static long anyToLong(Object v, long dv) {
    if (v == null) {
      return dv;
    } else if (v instanceof Integer) {
      return (long) (Integer) v;
    } else if (v instanceof Long) {
      return (Long) v;
    } else if (v instanceof Number) {
      return ((Number) v).longValue();
    } else {
      try {
        return Double.valueOf(v.toString()).longValue();
      } catch (Exception var4) {
        return dv;
      }
    }
  }
}